{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "955fe97d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0db3ebf0",
   "metadata": {},
   "source": [
    "### 获取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ea9e4e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据库地址：数据库放在上一级目录下\n",
    "db_path = os.path.join(os.path.dirname(os.getcwd()),\"data.db\")\n",
    "engine_path = \"sqlite:///\"+db_path\n",
    "\n",
    "# 获取数据函数，根据输入的SQL语句返回 DataFrame 类型数据\n",
    "def link_sqlite(sql):\n",
    "    engine = create_engine(engine_path)\n",
    "    df = pd.read_sql(sql,con=engine)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e33127ac",
   "metadata": {},
   "source": [
    "### 线性数据预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7d8a7c0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"select * from predictSalesSummary where shopid=1\"\n",
    "df = link_sqlite(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e0bb7ab5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>amount</th>\n",
       "      <th>shopid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2021年6月</td>\n",
       "      <td>23181.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2021年7月</td>\n",
       "      <td>27703.4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2021年8月</td>\n",
       "      <td>33041.2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2021年9月</td>\n",
       "      <td>38391.9</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2021年10月</td>\n",
       "      <td>44715.2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       month   amount  shopid\n",
       "7    2021年6月  23181.5       1\n",
       "8    2021年7月  27703.4       1\n",
       "9    2021年8月  33041.2       1\n",
       "10   2021年9月  38391.9       1\n",
       "11  2021年10月  44715.2       1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8a91571d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"14efbf5ded5b4da492ee6cba7e58709f\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_14efbf5ded5b4da492ee6cba7e58709f = echarts.init(\n",
       "                    document.getElementById('14efbf5ded5b4da492ee6cba7e58709f'), 'white', {renderer: 'canvas'});\n",
       "                var option_14efbf5ded5b4da492ee6cba7e58709f = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u9500\\u552e\\u989d\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                30894.5,\n",
       "                34456.8,\n",
       "                38812.3,\n",
       "                6644.7,\n",
       "                10639.1,\n",
       "                14498.7,\n",
       "                18660.9,\n",
       "                23181.5,\n",
       "                27703.4,\n",
       "                33041.2,\n",
       "                38391.9,\n",
       "                44715.2\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u9500\\u552e\\u989d\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u9500\\u552e\\u989d\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"axisLabel\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"rotate\": 20,\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"2020\\u5e7410\\u6708\",\n",
       "                \"2020\\u5e7411\\u6708\",\n",
       "                \"2020\\u5e7412\\u6708\",\n",
       "                \"2021\\u5e742\\u6708\",\n",
       "                \"2021\\u5e743\\u6708\",\n",
       "                \"2021\\u5e744\\u6708\",\n",
       "                \"2021\\u5e745\\u6708\",\n",
       "                \"2021\\u5e746\\u6708\",\n",
       "                \"2021\\u5e747\\u6708\",\n",
       "                \"2021\\u5e748\\u6708\",\n",
       "                \"2021\\u5e749\\u6708\",\n",
       "                \"2021\\u5e7410\\u6708\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5546\\u54c1\\u6bcf\\u4e2a\\u6708\\u9500\\u552e\\u989d\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_14efbf5ded5b4da492ee6cba7e58709f.setOption(option_14efbf5ded5b4da492ee6cba7e58709f);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1b07fa44340>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "\n",
    "x_names = df[\"month\"].tolist()\n",
    "tao_bao = df[\"amount\"].tolist()\n",
    "\n",
    "c = (\n",
    "    Bar()\n",
    "    .add_xaxis(x_names)\n",
    "    .add_yaxis(\"销售额\", tao_bao)\n",
    "    .set_global_opts(\n",
    "        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=20)),\n",
    "        title_opts=opts.TitleOpts(title=\"商品每个月销售额\"),\n",
    "    )\n",
    ")\n",
    "c.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a7de866f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从2021年2月份开始，数据呈现上升的线性趋势\n",
    "df2 = df.iloc[3:,:]\n",
    "\n",
    "# 数据复制一份，避免操作失误导致数据受损\n",
    "df3 = df2.copy()\n",
    "\n",
    "df3[\"x\"] = list(range(2,11))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fa1bd886",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>amount</th>\n",
       "      <th>shopid</th>\n",
       "      <th>x</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021年2月</td>\n",
       "      <td>6644.7</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021年3月</td>\n",
       "      <td>10639.1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2021年4月</td>\n",
       "      <td>14498.7</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2021年5月</td>\n",
       "      <td>18660.9</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2021年6月</td>\n",
       "      <td>23181.5</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2021年7月</td>\n",
       "      <td>27703.4</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2021年8月</td>\n",
       "      <td>33041.2</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2021年9月</td>\n",
       "      <td>38391.9</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2021年10月</td>\n",
       "      <td>44715.2</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       month   amount  shopid   x\n",
       "3    2021年2月   6644.7       1   2\n",
       "4    2021年3月  10639.1       1   3\n",
       "5    2021年4月  14498.7       1   4\n",
       "6    2021年5月  18660.9       1   5\n",
       "7    2021年6月  23181.5       1   6\n",
       "8    2021年7月  27703.4       1   7\n",
       "9    2021年8月  33041.2       1   8\n",
       "10   2021年9月  38391.9       1   9\n",
       "11  2021年10月  44715.2       1  10"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ff4857c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import linear_model\n",
    "from sklearn.metrics import mean_squared_error,r2_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f753f51f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([47636.39166667])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自变量数据\n",
    "x = df3[\"x\"].values.tolist()\n",
    "# 因变量数据\n",
    "y = df3[\"amount\"].values.tolist()\n",
    "# 由于自变量只有一个维度，需要改变一下数据结构\n",
    "x_reshape = np.array(x).reshape(-1,1)\n",
    "# 实例化一个线性模型\n",
    "lr = linear_model.LinearRegression()\n",
    "# 训练数据\n",
    "lr.fit(x_reshape,y)\n",
    "# 预测数据\n",
    "x_predict = np.array([11]).reshape(-1,1)\n",
    "lr.predict(x_predict)\n",
    "# array([47636.39166667])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a4d2976",
   "metadata": {},
   "source": [
    "### 非线性数据预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8f5403d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "shop_2_sql = \"select * from predictSalesSummary where shopid=2\"\n",
    "shop_2_df = link_sqlite(shop_2_sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "054ae975",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>amount</th>\n",
       "      <th>shopid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021年4月</td>\n",
       "      <td>4174.2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021年5月</td>\n",
       "      <td>3388.4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021年6月</td>\n",
       "      <td>1979.6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021年7月</td>\n",
       "      <td>2341.5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021年8月</td>\n",
       "      <td>2219.2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2021年9月</td>\n",
       "      <td>1972.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2021年10月</td>\n",
       "      <td>2091.9</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      month  amount  shopid\n",
       "0   2021年4月  4174.2       2\n",
       "1   2021年5月  3388.4       2\n",
       "2   2021年6月  1979.6       2\n",
       "3   2021年7月  2341.5       2\n",
       "4   2021年8月  2219.2       2\n",
       "5   2021年9月  1972.3       2\n",
       "6  2021年10月  2091.9       2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_2_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4851391c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"e1b5914364db4d768ac499ab74d4896f\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_e1b5914364db4d768ac499ab74d4896f = echarts.init(\n",
       "                    document.getElementById('e1b5914364db4d768ac499ab74d4896f'), 'white', {renderer: 'canvas'});\n",
       "                var option_e1b5914364db4d768ac499ab74d4896f = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u9500\\u552e\\u989d\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                4174.2,\n",
       "                3388.4,\n",
       "                1979.6,\n",
       "                2341.5,\n",
       "                2219.2,\n",
       "                1972.3,\n",
       "                2091.9\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u9500\\u552e\\u989d\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u9500\\u552e\\u989d\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"axisLabel\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"rotate\": 20,\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"2021\\u5e744\\u6708\",\n",
       "                \"2021\\u5e745\\u6708\",\n",
       "                \"2021\\u5e746\\u6708\",\n",
       "                \"2021\\u5e747\\u6708\",\n",
       "                \"2021\\u5e748\\u6708\",\n",
       "                \"2021\\u5e749\\u6708\",\n",
       "                \"2021\\u5e7410\\u6708\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5546\\u54c1\\u6bcf\\u4e2a\\u6708\\u9500\\u552e\\u989d\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_e1b5914364db4d768ac499ab74d4896f.setOption(option_e1b5914364db4d768ac499ab74d4896f);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1b012307b50>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "\n",
    "x_names = shop_2_df[\"month\"].tolist()\n",
    "tao_bao = shop_2_df[\"amount\"].tolist()\n",
    "\n",
    "c = (\n",
    "    Bar()\n",
    "    .add_xaxis(x_names)\n",
    "    .add_yaxis(\"销售额\", tao_bao)\n",
    "    .set_global_opts(\n",
    "        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=20)),\n",
    "        title_opts=opts.TitleOpts(title=\"商品每个月销售额\"),\n",
    "    )\n",
    ")\n",
    "c.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "895a4cf4",
   "metadata": {},
   "source": [
    "商品2的销售额，在最近5个月内变化幅度不大，可以用加权平均值的方法预测下个月销售额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6c0c32c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2093.44"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(shop_2_df.iloc[4:,1]*np.array([0.2,0.2,0.6]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "eaac9b45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2094.4666666666667"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_2_df.iloc[4:,1].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f77ce2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
